﻿using System;
using System.Collections.Generic;
using System.Text;
using Customs.Components.KDT.GC.Info;
using System.Data.SqlClient;
using System.Data;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;
namespace Customs.Components.KDT.GC
{
    public class PhuKienDangKy : PhuKienDangKyInfo
    {
        public void Load(PhuKienDangKyInfo entity)
        {
            this._ghichu = entity.GhiChu;
            this._MaDoanhNghiep = entity.MaDoanhNghiep;
            this._MaHaiQuan = entity.MaHaiQuan;
            this._MaPhuKien = entity.MaPhuKien;
            this._NamTiepNhan = entity.NamTiepNhan;
            this._NgayKy = entity.NgayKy;
            this._NgayPhuKien = entity.NgayPhuKien;
            this._NgayTiepNhan = entity.NgayTiepNhan;
            this._noidung = entity.NoiDung;
            this._SoHopDong = entity.SoHopDong;
            this._SoPhuKien = entity.SoPhuKien;
            this._SoTiepNhan = entity.SoTiepNhan;
            this._thongtincu = entity.ThongTinCu;
            this._thongtinmoi = entity.ThongTinMoi;
            this._trangthaixl = entity.TrangThaiXL;
            this._vbcp = entity.VBCP;
            this.HangPhuKien = entity.HangPhuKien;
        }


        private long getSoTiepNhan(SqlTransaction transaction)
        {
            string sqlName = "[sp_MASAN_tblCAPSO_PHUKIEN_GetSoMax]";

            SqlParameter[] parameters = new SqlParameter[3];

            parameters[0] = new SqlParameter("@Ma_HQTN", SqlDbType.Char, 6);
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = this._MaHaiQuan;

            parameters[1] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = this._NamTiepNhan;

            parameters[2] = new SqlParameter("@SoMax", SqlDbType.BigInt);
            parameters[2].Direction = ParameterDirection.Output;
            parameters[2].Value = 0;

            IDataReader reader= SqlHelper.ExecuteReader(transaction, CommandType.StoredProcedure, sqlName, parameters);

            long result = (long)parameters[2].Value;
            reader.Close();
            return result;
        }

        //-----------------------------------------------------------------------------------------
        public bool Load()
        {
            SqlDataReader reader = this.Select();
            if (reader.Read())
            {
                if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) this._SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHaiQuan"))) this._MaHaiQuan = reader.GetString(reader.GetOrdinal("MaHaiQuan"));
                if (!reader.IsDBNull(reader.GetOrdinal("Nam_TN"))) this._NamTiepNhan = reader.GetInt16(reader.GetOrdinal("Nam_TN"));
                if (!reader.IsDBNull(reader.GetOrdinal("So_HD"))) this._SoHopDong = reader.GetString(reader.GetOrdinal("So_HD"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDoanhNghiep"))) this._MaDoanhNghiep = reader.GetString(reader.GetOrdinal("MaDoanhNghiep"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ngay_Ky"))) this._NgayKy = reader.GetDateTime(reader.GetOrdinal("Ngay_Ky"));
                if (!reader.IsDBNull(reader.GetOrdinal("So_PK"))) this._SoPhuKien = reader.GetString(reader.GetOrdinal("So_PK"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ma_PK"))) this._MaPhuKien = reader.GetString(reader.GetOrdinal("Ma_PK"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ngay_PK"))) this._NgayPhuKien = reader.GetDateTime(reader.GetOrdinal("Ngay_PK"));
                if (!reader.IsDBNull(reader.GetOrdinal("So_TN"))) this.SoTiepNhanHQDuyet = reader.GetInt32(reader.GetOrdinal("So_TN"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ngay_TN"))) this._NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("Ngay_TN"));
                if (!reader.IsDBNull(reader.GetOrdinal("Noi_Dung"))) this._noidung = reader.GetString(reader.GetOrdinal("Noi_Dung"));
                if (!reader.IsDBNull(reader.GetOrdinal("VBCP"))) this._vbcp = reader.GetString(reader.GetOrdinal("VBCP"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ghi_Chu"))) this._ghichu = reader.GetString(reader.GetOrdinal("Ghi_Chu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) this._thongtincu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinMoi"))) this._thongtinmoi = reader.GetString(reader.GetOrdinal("ThongTinMoi"));
                if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXL"))) this._trangthaixl = reader.GetInt32(reader.GetOrdinal("TrangThaiXL"));
                if (!reader.IsDBNull(reader.GetOrdinal("So_TN"))) this._so_TN = reader.GetInt32(reader.GetOrdinal("So_TN"));
                //PhuKien pk = new PhuKien();
                //pk.SoHopDong = this._SoHopDong;
                //pk.MaDoanhNghiep = this._MaDoanhNghiep;
                //pk.MaHaiQuan = this._MaHaiQuan;
                //pk.SoPhuKien = this._SoPhuKien;
                //pk.NgayKy = this._NgayKy;
                //pk.MaPhuKien = this._MaPhuKien;
                //this.HangPhuKien = pk.selectHangOfLoaiPhuKien();
                reader.Close();
                return true;
            }
            reader.Close();
            return false;
        }

        public SqlDataReader Select()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString;
            StringBuilder query = new StringBuilder();
            query.Append("SELECT ");
            query.Append("So_TNDKDT AS [SoTiepNhan], ");
            query.Append("Ma_HQTN AS [MaHaiQuan], ");
            query.Append("Nam_TN AS [Nam_TN], ");
            query.Append("So_HD AS [So_HD], ");
            query.Append("DVGC AS [MaDoanhNghiep], ");
            query.Append("Ngay_Ky AS [Ngay_Ky], ");
            query.Append("So_PK AS [So_PK], ");
            query.Append("Ma_PK AS [Ma_PK], ");
            query.Append("Ngay_PK AS [Ngay_PK], ");
            query.Append("So_TN AS [So_TN], ");
            query.Append("Ngay_TN AS [Ngay_TN], ");
            query.Append("Noi_Dung AS [Noi_Dung], ");
            query.Append("VBCP AS [VBCP], ");
            query.Append("Ghi_chu AS [Ghi_Chu], ");
            query.Append("OLD_info AS [ThongTinCu], ");
            query.Append("New_info AS [ThongTinMoi], ");            
            query.Append("TrangThaiXL AS [TrangThaiXL] ");
            query.Append("FROM dbo.[tblDPKHDGC] ");
            query.Append("WHERE ");
            query.Append("So_TNDKDT = @So_TNDKDT AND Ma_HQTN = @Ma_HQTN AND Nam_TN = @Nam_TN");

            SqlParameter[] parameters = new SqlParameter[3];
            parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
            parameters[0].Value = this.SoTiepNhan;

            parameters[1] = new SqlParameter("@Ma_HQTN", SqlDbType.Char, 6);
            parameters[1].Value = this.MaHaiQuan;

            parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
            parameters[2].Value = this.NamTiepNhan;

        
            return SqlHelper.ExecuteReader(connectionString, CommandType.Text, query.ToString(), parameters);
        }
        #region Khai báo phụ kiện

        //---------------------------------------------------------------------------------------------
        //tạo mới các phụ kiện của một số phụ kiện vào cơ sở dữ liệu
        public void InsertTransaction(SqlTransaction transaction)
        {
            StringBuilder query = new StringBuilder();
            query.Append("INSERT INTO tblDPKHDGC ( ");
            query.Append("So_TNDKDT , ");
            query.Append("Ma_HQTN , ");
            query.Append("Nam_TN , ");
            query.Append("So_HD , ");
            query.Append("Ma_HQHD , ");
            query.Append("DVGC , ");
            query.Append("Ngay_Ky , ");
            query.Append("So_PK , ");
            query.Append("Ma_PK , ");
            query.Append("Ngay_PK , ");                        
            query.Append("Ngay_VM , ");
            query.Append("Noi_dung , ");
            query.Append("VBCP , ");
            query.Append("Ghi_chu , ");
            query.Append("Old_info , ");
            query.Append("New_info , ");
            query.Append("TrangThaiXL , ");
            query.Append("KieuKB , ");
            query.Append("Ma_DVKB ) ");

            query.Append("VALUES ( ");

            query.Append("@So_TNDKDT , ");
            query.Append("@Ma_HQTN , ");
            query.Append("@Nam_TN , ");
            query.Append("@So_HD , ");
            query.Append("@Ma_HQHD , ");
            query.Append("@DVGC , ");
            query.Append("@Ngay_Ky , ");
            query.Append("@So_PK , ");
            query.Append("@Ma_PK , ");
            query.Append("@Ngay_PK , ");          
            query.Append("@Ngay_VM , ");
            query.Append("@Noi_dung , ");
            query.Append("@VBCP , ");
            query.Append("@Ghi_chu , ");
            query.Append("@Old_info , ");
            query.Append("@New_info , ");
            query.Append("@TrangThaiXL , ");
            query.Append("@KieuKB , ");
            query.Append("@Ma_DVKB ) ");
            SqlParameter[] parameters = new SqlParameter[21];

            parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
            parameters[0].Value = this._SoTiepNhan;

            parameters[1] = new SqlParameter("@Ma_HQTN", SqlDbType.Char,6);
            parameters[1].Value = this._MaHaiQuan;

            parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
            parameters[2].Value = this._NamTiepNhan;

            parameters[3] = new SqlParameter("@So_HD", SqlDbType.VarChar);
            parameters[3].Value = this._SoHopDong;

            parameters[4] = new SqlParameter("@Ma_HQHD", SqlDbType.Char,6);
            parameters[4].Value = this._MaHaiQuan;

            parameters[5] = new SqlParameter("@DVGC", SqlDbType.VarChar, 14);
            parameters[5].Value = this._MaDoanhNghiep;

            parameters[6] = new SqlParameter("@Ngay_Ky", SqlDbType.DateTime);
            parameters[6].Value = this._NgayKy;

            parameters[7] = new SqlParameter("@So_PK", SqlDbType.VarChar);
            parameters[7].Value = this._SoPhuKien;

            parameters[8] = new SqlParameter("@Ma_PK", SqlDbType.VarChar,4);
            parameters[8].Value = this._MaPhuKien;

            parameters[9] = new SqlParameter("@Ngay_PK", SqlDbType.DateTime);
            parameters[9].Value = this._NgayPhuKien;
        

            parameters[12] = new SqlParameter("@Ngay_VM", SqlDbType.DateTime);
            parameters[12].Value = DateTime.Today;

            parameters[13] = new SqlParameter("@Noi_dung", SqlDbType.NVarChar);
            parameters[13].Value = this._noidung;

            parameters[14] = new SqlParameter("@VBCP", SqlDbType.NVarChar);
            parameters[14].Value = this._vbcp;

            parameters[15] = new SqlParameter("@Ghi_chu", SqlDbType.NVarChar);
            parameters[15].Value = this._ghichu;

            parameters[16] = new SqlParameter("@Old_info", SqlDbType.NVarChar);
            parameters[16].Value = this._thongtincu;

            parameters[17] = new SqlParameter("@New_info", SqlDbType.NVarChar);
            parameters[17].Value = this._thongtinmoi;

            parameters[18] = new SqlParameter("@TrangThaiXL", SqlDbType.Int);
            parameters[18].Value = 0;

            parameters[19] = new SqlParameter("@KieuKB", SqlDbType.Int);
            parameters[19].Value = 1;

            parameters[20] = new SqlParameter("@Ma_DVKB", SqlDbType.Char,14);
            parameters[20].Value = this._MaDoanhNghiep;
            SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, query.ToString(), parameters);
        }
        //Khai báo phụ kiện của hợp đồng
        public long KhaiBaoPhuKien(PhuKienDangKyInfoCollection collection)
        {           
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString))
            {
                conn.Open();
                SqlTransaction transaction = conn.BeginTransaction();

                try
                {
                    //xóa phụ kiện cũ đó nếu có  

                        this.Load(collection[0]);                      
                        if (this.SoTiepNhan > 0)
                        {
                            this.Load();
                            if (this.TrangThaiXL == 1)
                                throw new Exception("1");
                            this.Delete(transaction);
                            PhuKien pk = new PhuKien();
                            pk.Delete(this, transaction);
                        }
                  
                    //tạo mới phụ kiện
                    this.NamTiepNhan = DateTime.Now.Year;
                    this._SoTiepNhan = getSoTiepNhan(transaction);                    
           
                    foreach (PhuKienDangKyInfo entity in collection)
                    {
                        PhuKienDangKy pkdk = new PhuKienDangKy();
                        pkdk.Load(entity);
                        pkdk.SoTiepNhan = this.SoTiepNhan;
                        pkdk.NamTiepNhan = this.NamTiepNhan;
                        pkdk.InsertTransactionFull(transaction);                        
                    }
                    transaction.Commit();                    
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }
            return this.SoTiepNhan;
        }
        //Tạo mới phụ kiện và hàng của phụ kiện nếu có
        public void InsertTransactionFull(SqlTransaction transaction)
        {
            this.InsertTransaction(transaction);                
            PhuKien hangPK = new PhuKien();
            hangPK.InsertTransaction(transaction, this.HangPhuKien);
        }
        #endregion Khai báo phụ kiện

        #region Hủy phụ kiện
        public void Delete(SqlTransaction transaction)
        {
           string query = string.Format("DELETE FROM tblDPKHDGC WHERE So_TNDKDT = {0} AND Ma_HQTN = '{1}' AND Nam_TN = {2} ", this.SoTiepNhan, this.MaHaiQuan, this.NamTiepNhan);        
            SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, query);
        }
        
        public bool HuyPhuKien(PhuKienDangKyInfo entity)
        {
            bool returnValue = false;
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString))
            {
                conn.Open();
                SqlTransaction transaction = conn.BeginTransaction();

                try
                {
                    this.Load(entity);                    
                    this.Load();
                    if (this.TrangThaiXL == 1)
                        throw new Exception("1");
                    this.Delete(transaction);
                    PhuKien pk = new PhuKien();
                    pk.Delete(this, transaction);
                    transaction.Commit();
                    returnValue = true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }
            return returnValue;
        }
        #endregion Hủy phụ kiện

        #region Request_Status
        //public static PhuKienDangKyInfo Request_Status(string mahaiquan,long sotiepnhan,short namtiepnhan)
        //{
        //    string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString;
        //    StringBuilder query = new StringBuilder();
        //    query.Append("SELECT ");
        //    query.Append("So_TNDKDT AS [SoTiepNhan], ");
        //    query.Append("Ma_HQTN AS [MaHaiQuan], ");
        //    query.Append("Nam_TN AS [Nam_TN], ");
        //    query.Append("So_HD AS [So_HD], ");
        //    query.Append("DVGC AS [MaDoanhNghiep], ");
        //    query.Append("Ngay_Ky AS [Ngay_Ky], ");
        //    query.Append("So_PK AS [So_PK], ");
        //    query.Append("Ma_PK AS [Ma_PK], ");
        //    query.Append("Ngay_PK AS [Ngay_PK], ");
        //    query.Append("So_TN AS [So_TN], ");
        //    query.Append("Ngay_TN AS [Ngay_TN], ");
        //    query.Append("Noi_Dung AS [Noi_Dung], ");
        //    query.Append("VBCP AS [VBCP], ");
        //    query.Append("Ghi_chu AS [Ghi_Chu], ");
        //    query.Append("OLD_info AS [ThongTinCu], ");
        //    query.Append("New_info AS [ThongTinMoi], ");
        //    query.Append("TrangThaiXL AS [TrangThaiXL] ");
        //    query.Append("FROM dbo.[tblDPKHDGC] ");
        //    query.Append("WHERE ");
        //    query.Append("So_TNDKDT = @So_TNDKDT AND Ma_HQTN = @Ma_HQTN AND Nam_TN = @Nam_TN");

        //    SqlParameter[] parameters = new SqlParameter[3];
            
        //    parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
        //    parameters[0].Value = sotiepnhan;

        //    parameters[1] = new SqlParameter("@Ma_HQTN", SqlDbType.Char, 6);
        //    parameters[1].Value = mahaiquan;

        //    parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
        //    parameters[2].Value = namtiepnhan;

        //    SqlDataReader reader= SqlHelper.ExecuteReader(connectionString, CommandType.Text, query.ToString(), parameters);
                         
        //    if (reader.Read())
        //    {
        //        PhuKienDangKyInfo pkDangKy = new PhuKienDangKyInfo();
        //        if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) pkDangKy.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
        //        if (!reader.IsDBNull(reader.GetOrdinal("MaHaiQuan"))) pkDangKy.MaHaiQuan = reader.GetString(reader.GetOrdinal("MaHaiQuan"));
        //        if (!reader.IsDBNull(reader.GetOrdinal("Nam_TN"))) pkDangKy.NamTiepNhan = reader.GetInt16(reader.GetOrdinal("Nam_TN"));
        //        if (!reader.IsDBNull(reader.GetOrdinal("So_HD"))) pkDangKy.SoHopDong = reader.GetString(reader.GetOrdinal("So_HD"));
        //        if (!reader.IsDBNull(reader.GetOrdinal("MaDoanhNghiep"))) pkDangKy.MaDoanhNghiep = reader.GetString(reader.GetOrdinal("MaDoanhNghiep"));
        //        if (!reader.IsDBNull(reader.GetOrdinal("Ngay_Ky"))) pkDangKy.NgayKy = reader.GetDateTime(reader.GetOrdinal("Ngay_Ky"));
        //        if (!reader.IsDBNull(reader.GetOrdinal("So_PK"))) pkDangKy.SoPhuKien = reader.GetString(reader.GetOrdinal("So_PK"));
        //        if (!reader.IsDBNull(reader.GetOrdinal("Ma_PK"))) pkDangKy.MaPhuKien = reader.GetString(reader.GetOrdinal("Ma_PK"));
        //        if (!reader.IsDBNull(reader.GetOrdinal("Ngay_PK"))) pkDangKy.NgayPhuKien = reader.GetDateTime(reader.GetOrdinal("Ngay_PK"));
        //        if (!reader.IsDBNull(reader.GetOrdinal("So_TN"))) pkDangKy.SoTiepNhan = reader.GetInt32(reader.GetOrdinal("So_TN"));
        //        if (!reader.IsDBNull(reader.GetOrdinal("Ngay_TN"))) pkDangKy.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("Ngay_TN"));
        //        if (!reader.IsDBNull(reader.GetOrdinal("Noi_Dung"))) pkDangKy.NoiDung = reader.GetString(reader.GetOrdinal("Noi_Dung"));
        //        if (!reader.IsDBNull(reader.GetOrdinal("VBCP"))) pkDangKy.VBCP = reader.GetString(reader.GetOrdinal("VBCP"));
        //        if (!reader.IsDBNull(reader.GetOrdinal("Ghi_Chu"))) pkDangKy.GhiChu = reader.GetString(reader.GetOrdinal("Ghi_Chu"));
        //        if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) pkDangKy.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
        //        if (!reader.IsDBNull(reader.GetOrdinal("ThongTinMoi"))) pkDangKy.ThongTinMoi = reader.GetString(reader.GetOrdinal("ThongTinMoi"));
        //        if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXL"))) pkDangKy.TrangThaiXL = reader.GetInt32(reader.GetOrdinal("TrangThaiXL"));
        //        reader.Close();
        //        return pkDangKy;
        //    }
        //    reader.Close();
        //    return null;
        //}
        public static PhuKienDangKyInfoCollection Request_Status(string mahaiquan, long sotiepnhan, short namtiepnhan)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString;
            StringBuilder query = new StringBuilder();
            query.Append("SELECT ");
            query.Append("So_TNDKDT AS [SoTiepNhan], ");
            query.Append("Ma_HQTN AS [MaHaiQuan], ");
            query.Append("Nam_TN AS [Nam_TN], ");
            query.Append("So_HD AS [So_HD], ");
            query.Append("DVGC AS [MaDoanhNghiep], ");
            query.Append("Ngay_Ky AS [Ngay_Ky], ");
            query.Append("So_PK AS [So_PK], ");
            query.Append("Ma_PK AS [Ma_PK], ");
            query.Append("Ngay_PK AS [Ngay_PK], ");
            query.Append("So_TN AS [So_TN], ");
            query.Append("Ngay_TN AS [Ngay_TN], ");
            query.Append("Noi_Dung AS [Noi_Dung], ");
            query.Append("VBCP AS [VBCP], ");
            query.Append("Ghi_chu AS [Ghi_Chu], ");
            query.Append("OLD_info AS [ThongTinCu], ");
            query.Append("New_info AS [ThongTinMoi], ");
            query.Append("TrangThaiXL AS [TrangThaiXL] ");
            query.Append("FROM dbo.[tblDPKHDGC] ");
            query.Append("WHERE ");
            query.Append("So_TNDKDT = @So_TNDKDT AND Ma_HQTN = @Ma_HQTN AND Nam_TN = @Nam_TN");

            SqlParameter[] parameters = new SqlParameter[3];

            parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
            parameters[0].Value = sotiepnhan;

            parameters[1] = new SqlParameter("@Ma_HQTN", SqlDbType.Char, 6);
            parameters[1].Value = mahaiquan;

            parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
            parameters[2].Value = namtiepnhan;

            SqlDataReader reader = SqlHelper.ExecuteReader(connectionString, CommandType.Text, query.ToString(), parameters);


            PhuKienDangKyInfoCollection collection = new PhuKienDangKyInfoCollection();
            while (reader.Read())
            {
                PhuKienDangKyInfo pkDangKy = new PhuKienDangKyInfo();
                if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) pkDangKy.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHaiQuan"))) pkDangKy.MaHaiQuan = reader.GetString(reader.GetOrdinal("MaHaiQuan"));
                if (!reader.IsDBNull(reader.GetOrdinal("Nam_TN"))) pkDangKy.NamTiepNhan = reader.GetInt16(reader.GetOrdinal("Nam_TN"));
                if (!reader.IsDBNull(reader.GetOrdinal("So_HD"))) pkDangKy.SoHopDong = reader.GetString(reader.GetOrdinal("So_HD"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDoanhNghiep"))) pkDangKy.MaDoanhNghiep = reader.GetString(reader.GetOrdinal("MaDoanhNghiep"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ngay_Ky"))) pkDangKy.NgayKy = reader.GetDateTime(reader.GetOrdinal("Ngay_Ky"));
                if (!reader.IsDBNull(reader.GetOrdinal("So_PK"))) pkDangKy.SoPhuKien = reader.GetString(reader.GetOrdinal("So_PK"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ma_PK"))) pkDangKy.MaPhuKien = reader.GetString(reader.GetOrdinal("Ma_PK"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ngay_PK"))) pkDangKy.NgayPhuKien = reader.GetDateTime(reader.GetOrdinal("Ngay_PK"));
                if (!reader.IsDBNull(reader.GetOrdinal("So_TN"))) pkDangKy.SoTiepNhanHQDuyet = reader.GetInt32(reader.GetOrdinal("So_TN"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ngay_TN"))) pkDangKy.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("Ngay_TN"));
                if (!reader.IsDBNull(reader.GetOrdinal("Noi_Dung"))) pkDangKy.NoiDung = reader.GetString(reader.GetOrdinal("Noi_Dung"));
                if (!reader.IsDBNull(reader.GetOrdinal("VBCP"))) pkDangKy.VBCP = reader.GetString(reader.GetOrdinal("VBCP"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ghi_Chu"))) pkDangKy.GhiChu = reader.GetString(reader.GetOrdinal("Ghi_Chu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) pkDangKy.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinMoi"))) pkDangKy.ThongTinMoi = reader.GetString(reader.GetOrdinal("ThongTinMoi"));
                if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXL"))) pkDangKy.TrangThaiXL = reader.GetInt32(reader.GetOrdinal("TrangThaiXL"));
                if (!reader.IsDBNull(reader.GetOrdinal("So_TN"))) pkDangKy.SoTiepNhanHQDuyet = reader.GetInt32(reader.GetOrdinal("So_TN"));
             
                PhuKien pk = new PhuKien();
                pk.SoHopDong = pkDangKy.SoHopDong;
                pk.MaDoanhNghiep = pkDangKy.MaDoanhNghiep;
                pk.MaHaiQuan = pkDangKy.MaHaiQuan;
                pk.SoPhuKien = pkDangKy.SoPhuKien;
                pk.NgayKy = pkDangKy.NgayKy;
                pk.MaPhuKien = pkDangKy.MaPhuKien;
                pkDangKy.HangPhuKien = pk.selectHangOfLoaiPhuKien();
                collection.Add(pkDangKy);
            }
            reader.Close();
            return collection;
        }
      
   
        #endregion Request_Status
    }
}
